package com.gxa.worker.recycling.service.impl;

import com.gxa.core.recycling.common.ExceptionCode;
import com.gxa.core.recycling.exception.BusinessException;
import com.gxa.core.recycling.pojo.ChangePointsStandard;
import com.gxa.core.recycling.pojo.User;
import com.gxa.core.recycling.pojo.Waste;
import com.gxa.core.recycling.pojo.WasteType;
import com.gxa.core.recycling.util.StringUtil;
import com.gxa.worker.recycling.mapper.ChangePointStandardMapper;
import com.gxa.worker.recycling.mapper.UserMapper;
import com.gxa.worker.recycling.mapper.WasteMapper;
import com.gxa.worker.recycling.service.WasteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author ：looker
 * @date ：Created in 2021/12/4 18:50
 */
@Service
public class WasteServiceImpl implements WasteService {

    @Autowired
    WasteMapper wasteMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    ChangePointStandardMapper changePointStandardMapper;

    @Override
    public List<WasteType> getWasteType() {
        List<WasteType> allWasteType = wasteMapper.getAllWasteType();
        return allWasteType;
    }

    @Override
    public List<Waste> getWasteByWasteType(Long id) {
        Example example = new Example(Waste.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("wasteTypeId",id);
        List<Waste> wastes = wasteMapper.selectByExample(example);
        return wastes;
    }

    @Override
    public Integer getExhangePoint(Long id, Long wasteId) throws BusinessException {
        //取出用户等级
        User user = userMapper.selectByPrimaryKey(id);
        String levelId = user.getLevelId();
        if (StringUtil.isEmpty(levelId)) {
            throw new BusinessException(ExceptionCode.LEVEL_ERROR);
        }
        Example example = new Example(ChangePointsStandard.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("vipId",levelId).andEqualTo("wasteId",wasteId);
        List<ChangePointsStandard> changePointList = changePointStandardMapper.selectByExample(example);
        if (changePointList.size()<0) {
            throw new BusinessException(ExceptionCode.LEVEL_ERROR);
        }
        //该用户自己兑换该单位商品所需积分
        Integer changeNum = changePointList.get(0).getChangeNum();

        return changeNum;
    }
}
